<?php 
	session_start();
	include('include/db_connect.php');
	include("include/header.php"); 
	include("include/menu.php");
	if(!isset($_GET['sub1']) && !isset($_GET['sub2'])) {
		header("Location: productoverzicht.php?sub1=&sub2=");
	}
	echo "<div id='main_content'>";
	
	echo "<div id='super_main_content'>";
	echo "<h2>Productoverzicht</h2>";
	$textQuery = mysql_query("SELECT `omschrijving` FROM `PAGINAOMSCHRIJVING` WHERE `naam_pagina` = 'productoverzicht.php'");
	if(!$textQuery) {
		die("Query failed. Please try again later.");
	}
	$welcomeText = mysql_result($textQuery, 0);
	echo $welcomeText;
	echo "<div id='product_overview'>";
	echo "<table id='product_overview'>";
	if($_GET['sub1'] != "") {
		if($_GET['sub2'] == "") {
			$res = mysql_query("SELECT DISTINCT `sub2` AS 'category' ".
			                   "FROM `PRODUCT` ".
			                   "WHERE `sub1` = '".$_GET['sub1']."'");
			if(!$res) {
				die("Query failed. Please try again later. ".mysql_error());
			}
			printCategory($res, 2, $_GET['sub1']);
		} else if($_GET['sub2'] != "") {
			$res = mysql_query("SELECT DISTINCT `sub3` AS 'category' ".
			                   "FROM `PRODUCT` ".
			                   "WHERE `sub1` = '".$_GET['sub1']."' ".
			                   "AND `sub2` = '".$_GET['sub2']."'");
			if(!$res) {
				die("Query failed. Please try again later. ".mysql_error());
			}
			if(mysql_result($res, 0) == NULL) {
				header("Location: product.php?sub1=".$_GET['sub1']."&sub2=".$_GET['sub2']);
			}
			printCategory($res, 3, $_GET['sub1'], $_GET['sub2']);
		}
	} else {
		$res = mysql_query("SELECT DISTINCT `sub1` AS 'category' FROM `PRODUCT` ORDER BY `category`");
		if(!$res) {
			die("Query failed. Please try again later. ".mysql_error());
		}
		printCategory($res, 1);
	}
	
	//functie om de categorie tabel uit te printen
	function printCategory($res, $iSub, $sub1 = '', $sub2 = '', $sub3 = '') {
		$i = 1;
		while($row = mysql_fetch_assoc($res)) {
			switch($iSub) {
				case 1: 
					$sub1 = $row['category'];
					$htCategory = "<a href='?sub1=".$sub1."&sub2=".$sub2."'>".$row['category']."</a>";
					$catQuery = mysql_query("SELECT `sub2` FROM `PRODUCT` WHERE `sub1` = '".$sub1."'");
					if(!$catQuery) {
						die("Query failed. ".mysql_error());
					}
					if(mysql_result($catQuery, 0) == NULL) {
						$htCategory = "<a href='product.php?sub1=".$sub1."'>".$row['category']."</a>";
					}
					break;
				case 2: 
					$sub2 = $row['category'];
					$htCategory = "<a href='?sub1=".$sub1."&sub2=".$sub2."'>".$row['category']."</a>";
					$catQuery = mysql_query("SELECT `sub3` FROM `PRODUCT` WHERE `sub2` = '".$sub2."'");
					if(!$catQuery) {
						die("Query failed. ".mysql_error());
					}
					if(mysql_result($catQuery, 0) == NULL) {
						$htCategory = "<a href='product.php?sub1=".$sub1."&sub2=".$sub2."'>".$row['category']."</a>";
					}
					break;
				case 3:
					$sub3 = $row['category'];
					$htCategory = "<a href='product.php?sub1=".$sub1."&sub2=".$sub2."&sub3=".$sub3."'>".$row['category']."</a>";
					break;
			}
			if($i % 2 == 1 ) {
				echo "<tr>";
			} 
			echo "<td>";
			echo $htCategory;
			echo "</td>";
			if($i % 2 == 0) {
				echo "</tr>";
			}
			$i++;
		}
	}
	
	echo "</table>";
	echo "</div>";
	echo "</div>";
	
	echo "</div>";
	echo "<div class='clearthis'></div>";
	include('include/footer.php');
?>